﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SpringStudio.Common;
using SpringStudio.DAL.PaginationHandler;
using Sdepci.InfoCode.BLL.Authority;

namespace Sdepci.InfoCode.Web.Authority
{
    public partial class UserList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.ViewState["MyGridTemplate"] = this.MyGridTemplate.InnerHtml;
                this.BindData();
            }
        }

        /// <summary>
        /// 绑定数据列表
        /// </summary>
        private void BindData()
        {
            Dictionary<string, string> roleDict = new RoleManager().GetRoleDict();  // 获取角色集合
            // 绑定到下拉列表
            this.RoleGuid.DataSource = roleDict;
            this.RoleGuid.DataValueField = "Key";
            this.RoleGuid.DataTextField = "Value";
            this.RoleGuid.DataBind();
            this.RoleGuid.Items.Insert(0, new ListItem("=角色不限=", ""));

            this.Pagination1.TableName = "InfoCodeUsers";
            this.Pagination1.PrimaryKeys.Add("UserId");
            this.Pagination1.OrderFields.Add(new PaginationOrderField("UserId", "Asc"));
            this.Pagination1.SelectCondition = "(1=1)";

            if (this.Request.QueryString["UserId"] != null && this.Request.QueryString["UserId"].Trim().Length > 0)
            {
                this.UserId.Text = this.Request.QueryString["UserId"].Trim();
                this.Pagination1.SelectCondition += " And UserId Like @UserId";
                this.Pagination1.SqlParameters.Add(new PaginationSqlParameter("@UserId", "%" + this.Request.QueryString["UserId"].Trim() + "%"));
            }

            if (this.Request.QueryString["UserName"] != null && this.Request.QueryString["UserName"].Trim().Length > 0)
            {
                this.UserName.Text = this.Request.QueryString["UserName"].Trim();
                this.Pagination1.SelectCondition += " And UserName Like @UserName";
                this.Pagination1.SqlParameters.Add(new PaginationSqlParameter("@UserName", "%" + this.Request.QueryString["UserName"].Trim() + "%"));
            }

            if (this.Request.QueryString["RoleGuid"] != null && this.Request.QueryString["RoleGuid"].Trim().Length > 0)
            {
                this.RoleGuid.Text = this.Request.QueryString["RoleGuid"].Trim();
                this.Pagination1.SelectCondition += " And RoleGuid=@RoleGuid";
                this.Pagination1.SqlParameters.Add(new PaginationSqlParameter("@RoleGuid", this.Request.QueryString["RoleGuid"].Trim()));
            }


            this.Pagination1.InitHtml();

            DataSet ds = this.Pagination1.PageDataSet;
            ds.Tables[0].Columns.Add("RoleName");
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                if (roleDict.ContainsKey(row["RoleGuid"].ToString()))
                {
                    row["RoleName"] = roleDict[row["RoleGuid"].ToString()].ToString();
                }
                else
                {
                    row["RoleName"] = "<span style=\"color:#9f9f9f;\">默认角色</font>";
                }
            }

            this.MyGridTemplate.InnerHtml = MyGrid.DsToHtml(ds, this.ViewState["MyGridTemplate"].ToString(), this.Pagination1.PageSize, this.Pagination1.PageIndex);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.Request.Form["ItemId"] != null)
            {
                UserManager userManager = new UserManager();
                string[] userIds = this.Request.Form["ItemId"].ToString().Split(',');
                bool result = true;
                for (int i = 0; i < userIds.Length; i++)
                {
                    if (!userManager.Delete(userIds[i]))
                    {
                        result = false;
                    }
                }

                string msg = result ? "删除完成！" : "操作完成，但未能把所选用户全部删除，请查看错误日志！";
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script language=\"javascript\">alert(\"" + msg + "\");</script>");
                this.BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string urlParameter = string.Empty;

            if (this.UserId.Text.Trim().Length > 0)
            {
                urlParameter += "UserId=" + this.UserId.Text.Trim() + "&";
            }

            if (this.UserName.Text.Trim().Length > 0)
            {
                urlParameter += "UserName=" + this.UserName.Text.Trim() + "&";
            }

            if (this.RoleGuid.Text.Trim().Length > 0)
            {
                urlParameter += "RoleGuid=" + this.RoleGuid.Text.Trim() + "&";
            }

            if (!"".Equals(urlParameter))
            {
                this.Response.Redirect("UserList.aspx?" + urlParameter.Substring(0,urlParameter.Length - 1));
            }
            else
            {
                this.Response.Redirect("UserList.aspx");
            }
        }
    }
}
